package com.example.smartcity.ui.selfCenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.SelfCenterListAdapter;
import com.example.smartcity.ui.Bean.SelfCenterMessage;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.example.smartcity.ui.guide.login;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class SelfCenter extends Fragment {
    SharedPreferences sharedPreferences;
    private String TAG = "TAG";
    private ImageView IV_SelfPhoto;
    private TextView TV_SelfCenterName;
    private ListView LV_Function;
    private Button btn_Out;
    String item[] = {"个人设置","我的订单","修改密码","意见反馈"};
    String userName = null;
    String userPhoto = null;
    String userPhone = null;
    String userSex =null;
    String IDCard = null;
    int userID = 0;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    if (userPhoto.length()>0) {
                        Glide.with(SelfCenter.this.getContext())
                                .load(OkHttpUtils.getBaseUrl(getContext())+userPhoto)
                                .transform(new CircleCrop())
                                .into(IV_SelfPhoto);
                    }
                    TV_SelfCenterName.setText(userName.length()>0?userName:"请登录");
                    break;
                case 2:

                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root =  inflater.inflate(R.layout.fragment_self_center, container, false);
        IV_SelfPhoto = root.findViewById(R.id.IV_SelfPhoto);
        TV_SelfCenterName = root.findViewById(R.id.TV_selfCenter);
        LV_Function = root.findViewById(R.id.LV_function);
        btn_Out = root.findViewById(R.id.btn_out);
        initView();
        return root;
    }
    public void initView(){
        //加载头像与用户名
        String newUrl = OkHttpUtils.getBaseUrl(getContext()) + "/getInfo";
        OkHttpUtils.getCall(newUrl,new File(getContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Gson gson = new Gson();
                stateBean sBean = gson.fromJson(json,stateBean.class);
                if (sBean.getCode()==200){
                    SelfCenterMessage MessageBean = gson.fromJson(json,SelfCenterMessage.class);
                    SelfCenterMessage.UserBean userBean = MessageBean.getUser();
                    userName = userBean.getNickName();
                    userPhone = userBean.getPhonenumber();
                    userSex = userBean.getSex();
                    userPhoto = userBean.getAvatar();
                    IDCard = (String)userBean.getIdCard();
                    userID = userBean.getUserId();
                    Message message = new Message();
                    message.what =0;
                    handler.sendMessage(message);
                    SharedPreferences.Editor editor = getContext().getSharedPreferences("selfMessage", Context.MODE_PRIVATE).edit();
                    editor.putString("userName",userName);
                    editor.putString("userPhone",userPhone);
                    editor.putString("userSex",userSex);
                    editor.putString("IDCard",IDCard);
                    editor.putString("userPhoto",userPhoto);
                    editor.putString("userID",String.valueOf(userID));
                    editor.apply();
                }else{
                    SharedPreferences.Editor editor= getContext().getSharedPreferences("First",Context.MODE_PRIVATE).edit();
                    editor.putBoolean("IsFirst",false);
                    editor.apply();
                    TV_SelfCenterName.post(new Runnable() {
                        @Override
                        public void run() {
//                            AlertDialog alertDialog = new AlertDialog
//                                    .Builder(getContext())
//                                    .setTitle("提示")
//                                    .setMessage("登录过期请重新登录")
//                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                                        @Override
//                                        public void onClick(DialogInterface dialog, int which) {
//                                            Message message = new Message();
//                                            message.what = 2;
//                                            handler.sendMessage(message);
//                                        }
//                                    })
//                                    .setCancelable(false)
//                                    .show();
                            Intent intent = new Intent(SelfCenter.this.getActivity(), login.class);
                            startActivity(intent);
                        }
                    });
                }
            }
        });

        LV_Function.setAdapter(new SelfCenterListAdapter(this.getContext(),R.layout.selfcenteritem,item));
        LV_Function.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(SelfCenter.this.getContext(),SelfMessageSetC.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 = new Intent(SelfCenter.this.getContext(),MyOrder.class);
                        startActivity(intent1);
                        break;
                    case 2:
                        startActivity(new Intent(SelfCenter.this.getContext(),ChangePassword.class));
                        break;
                    case 3:
                        startActivity(new Intent(SelfCenter.this.getContext(),Feedback.class));
                        break;
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }
}